package com.example.factory.api.dto;

import com.example.factory.api.constant.ErrorCode;

/**
 * @author Lei Tao
 */
public class Response<D> {

    private int status;

    private String msg;

    private D data;

    public Response(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public Response(int status, String msg, D data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public static <D> Response<D> failed(int status) {
        return new Response<>(status, ErrorCode.msg(status));
    }

    public static <D> Response<D> failed(int status, String msg) {
        return new Response<>(status, msg);
    }

    public static <D> Response<D> success(D data) {
        return new Response<>(0, "success", data);
    }

    public static <D> Response<D> success() {
        return new Response<>(0, "success", null);
    }

    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public D getData() {
        return data;
    }

    public boolean isSuccess() {
        return status == 0;
    }
}